package compile_and_download

import (
	"log"
	"os/exec"
	"test/unit_function/flash_device"
	"test/unit_function/sshdata"
)

type Rk3568Img struct {
	BuildTarget
	ImgLink string
}

// Compile 编译任务：上传脚本，执行脚本，下载产物
func (receiver *Rk3568Img) Compile() error {
	client := sshdata.GetConnection()
	defer client.Close()
	result := sshdata.UploadFile("/mnt/data/ywx1293154/script_img.sh", "script/script_img.sh", client, "0744")
	if !result {
		log.Println("upload file script_img failed")
	}
	log.Println("uploadFile succeed")
	session, err := client.NewSession()
	if err != nil {
		log.Println("Rk3568Img newSession failed, error=", err)
		return err
	}
	defer session.Close()
	log.Println("executing script_img.sh")
	err = session.Run("~/script_img.sh > ~/script_img.log")
	if err != nil {
		log.Println("Rk3568Img run script failed, error=", err)
		return err
	}
	log.Println("executing script_img.sh succeed!")
	return nil
}
func (receiver *Rk3568Img) Download() error {
	receiver.ImgLink = "Building Img"
	// 下载文件并解压
	client := sshdata.GetConnection()
	defer client.Close()
	sshdata.DownloadFile("/mnt/data/ywx1293154/openharmony/out/rk3568/packages/phone/image.tar.gz", "D:/auto_test/img/image.tar.gz", client)
	cmd := exec.Command("tar", "-zxvf", "image.tar.gz")
	cmd.Dir = "D:/auto_test/img"
	err := cmd.Run()
	if err != nil {
		log.Println("error occurred when executing tar -zxvf image.tar.gz ", err)
		return err
	}
	log.Println("executing tar -zxvf image.tar.gz succeed")
	log.Println("downloading img succeed!")
	return nil
}

func (receiver *Rk3568Img) Execute() error {
	flash_device.FlashDevice("D:/DeployDevice/resource/RK3568_tool/upgrade_tool.exe", "D:/auto_test/img/images")
	return nil
}

func (receiver *Rk3568Img) GetDownloadLink() string {
	return "Build Img"
}
